package com.dlc.shop.user.platform.controller;


import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dlc.shop.common.annotation.SysLog;
import com.dlc.shop.common.bean.SysConfig;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.i18n.I18nMessage;
import com.dlc.shop.common.response.ServerResponseEntity;
import com.dlc.shop.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


/**
 * 其他积分配置
 * @author lhd
 */
@Tag(name = "其他积分配置")
@RestController
@RequestMapping("/user/scoreExplainConfig")
@RequiredArgsConstructor
public class ScoreOtherConfigController {
    private final SysConfigService sysConfigService;
    @Value("${yami.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/info/{key}")
    @PreAuthorize("@pms.hasPermission('score:explainConfig:info')")
    @Operation(summary = "配置信息" , description = "配置信息")
    @Parameter(name = "key", description = "参数名" )
    public ServerResponseEntity<SysConfig> info(@PathVariable("key") String key){
        SysConfig config = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>().eq(SysConfig::getParamKey,key));
        if(Objects.isNull(config)){
            return ServerResponseEntity.success(new SysConfig());
        }
        return ServerResponseEntity.success(config);
    }

    @SysLog("保存配置")
    @PostMapping
    @Operation(summary = "保存配置" , description = "保存配置")
    @PreAuthorize("@pms.hasPermission('score:explainConfig:save')")
    public ServerResponseEntity<Void> save(@RequestBody  SysConfig config){
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        if (sysConfigService.count(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamKey, config.getParamKey())) > 0) {
            // 系统已存在相同配置的key
            throw new YamiShopBindException("yami.same.key");
        }
        sysConfigService.save(config);
        sysConfigService.removeSysConfig(config.getParamKey());
        return ServerResponseEntity.success();
    }

    @SysLog("修改配置")
    @PutMapping
    @Operation(summary = "修改配置" , description = "修改配置")
    @PreAuthorize("@pms.hasPermission('score:explainConfig:update')")
    public ServerResponseEntity<Void> update(@RequestBody @Valid SysConfig config){
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg(I18nMessage.getMessage("yami.operate.auth"));
        }
        SysConfig dbSysConfig = sysConfigService.getOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamKey, config.getParamKey()));
        dbSysConfig.setParamValue(config.getParamValue());
        sysConfigService.updateById(dbSysConfig);
        sysConfigService.removeSysConfig(config.getParamKey());
        return ServerResponseEntity.success();
    }


}
